package com.sofoscorp.android.gestcobranzamobile;

import java.text.DecimalFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class DatosClienteActivity extends Activity {
	//Element UI
	private Spinner CmbTipoOper;
	
	//Definir Objeto Intent
	private Intent go_intent = null;
	private Bundle go_bundle = null;
	
	//Definir Element UI
	private EditText TxtRIF;
	private EditText TxtTelf;
	private EditText TxtCondpago;
	private EditText TxtLimiteCred;
	private EditText TxtLimiteCredDisp;
	private EditText TxtPorcUtil;
	private EditText TxtPersContact;
	private EditText TxtName1;
	private EditText TxtDireccion;
	
	//Definir Objeto BO y WA
	private KNKK_Str gs_KNKK;
	private KNVK_Str gs_KNVK;
	private TVTA_Str gs_TVTA;
	private TVTA_BO go_TVTA_BO;
	private KNKK_BO go_KNKK_BO;
	private KNVK_BO go_KNVK_BO;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		ArrayAdapter<String> lo_Adaptador = null;
		String[] lt_TipoOper = {"-Seleccione-","Crear Anticipo","Consulta/Tratar Facturas","Reporte"};
		String lv_BUKRS = null;
		String lv_KUNNR = null;
		Double lv_KLPRZ;
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_datos_cliente);
				
		//Instanciar objeto con Element UI
		CmbTipoOper = (Spinner)findViewById(R.id.CmbTipoOper);
		
		//Obtener los datos del bundle
		go_bundle = this.getIntent().getExtras();
		
		//Crear Objetos BO
		go_TVTA_BO = new TVTA_BO();
		go_KNVK_BO = new KNVK_BO();
		go_KNKK_BO = new KNKK_BO();
		
		//Obtener Sociedad y Nro  de Cliente
		lv_BUKRS = go_bundle.getString("BUKRS");
		lv_KUNNR = go_bundle.getString("KUNNR");
		
		//Buscar datos de Persona contacto
		try {
			gs_KNVK = go_KNVK_BO.get_SingleByKey(lv_KUNNR);
		} catch (Exception e) {
		}
		
		//Buscar el area de control del credito
		try {
			gs_TVTA = go_TVTA_BO.get_SingleByKey(lv_BUKRS, lv_KUNNR);
		} catch (Exception e) {
		}
		
		//Buscar los control del credito cliente
		try {
			if (go_KNKK_BO.do_conn()) {
				gs_KNKK = go_KNKK_BO.get_single_by_key(lv_KUNNR, gs_TVTA.getKKBER());
			}
		} catch (Exception e) {
		}
		
		
		//Instanciar Element UI
		TxtName1 = (EditText)findViewById(R.id.TxtName1);
        TxtDireccion = (EditText)findViewById(R.id.TxtDireccion);
		TxtRIF = (EditText)findViewById(R.id.TxtRIF);
        TxtTelf = (EditText)findViewById(R.id.TxtTelf);
        TxtCondpago = (EditText)findViewById(R.id.TxtCondpago);
        TxtLimiteCred = (EditText)findViewById(R.id.TxtLimiteCred);
        TxtLimiteCredDisp = (EditText)findViewById(R.id.TxtLimiteCredDisp);
        TxtPorcUtil = (EditText)findViewById(R.id.TxtPorcUtil);
        
        // Llenar Element UI
        TxtName1.setText(go_bundle.getString("NAME1")+go_bundle.getString("NAME2"));
        TxtDireccion.setText(go_bundle.getString("DIRECCION"));
        TxtRIF.setText(go_bundle.getString("STCD1"));
        TxtTelf.setText(go_bundle.getString("TLF"));
        TxtCondpago.setText(go_bundle.getString("ZTERM")+"-"+go_bundle.getString("TEXTO"));
        
        try {
        	TxtLimiteCred.setText(gs_TVTA.getWAERS() + " " + formatDecimal(gs_KNKK.getKLIMK()));	
		} catch (Exception e) {
			TxtLimiteCred.setText(formatDecimal(0));
		}        
        
        try {
        	TxtLimiteCredDisp.setText(gs_TVTA.getWAERS() + " " + formatDecimal(gs_KNKK.getZSAUF()));	
		} catch (Exception e) {
			TxtLimiteCredDisp.setText(formatDecimal(0));
		}
		
		try {
			lv_KLPRZ = gs_KNKK.getZSAUF() / gs_KNKK.getKLIMK() * 100;
			TxtPorcUtil.setText(formatDecimal(lv_KLPRZ)+ " %");
		} catch (Exception e) {
			TxtPorcUtil.setText("0 %");
		}
		
		TxtPersContact = (EditText)findViewById(R.id.TxtPersContact);
		TxtPersContact.setText(go_bundle.getString("NAMEV"));
		
		//Llenar Spinner
		lo_Adaptador = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lt_TipoOper);
		lo_Adaptador.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
		CmbTipoOper.setAdapter(lo_Adaptador);
		
		CmbTipoOper.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> adapter, View view,
					int position, long arg3) {
				Bundle lo_bundle = new Bundle();
				lo_bundle.putString("NAME", go_bundle.getString("NAME1").toString());
				lo_bundle.putString("BUKRS", go_bundle.getString("BUKRS").toString());
				lo_bundle.putString("KUNNR", go_bundle.getString("KUNNR").toString());
				switch (position) {
				case 1:
					go_intent = new Intent(DatosClienteActivity.this,CrearAnticipoActivity.class);
					go_intent.putExtras(lo_bundle);
					startActivity(go_intent);
					break;
				case 2:
					
					break;
				case 3:
					
					break;
				default:
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}	
	
	private String formatDecimal(double number) {
		DecimalFormat lv_DF = new DecimalFormat("###,###,###,##0.00");
		String lv_formatted = lv_DF.format(number);
		return lv_formatted;
	}

	
}
